package com.talos.ums.service.appversion;

import com.talos.framework.common.pojo.PageResult;
import com.talos.framework.common.util.object.BeanUtils;
import com.talos.ums.controller.admin.appversion.vo.UmsAppVersionPageReqVO;
import com.talos.ums.controller.admin.appversion.vo.UmsAppVersionSaveReqVO;
import com.talos.ums.dal.dataobject.appversion.UmsAppVersionDO;
import com.talos.ums.dal.mysql.appversion.UmsAppVersionMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import static com.talos.framework.common.exception.enums.GlobalErrorCodeConstants.DATA_NOT_FOUND;
import static com.talos.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 移动应用版本管理 Service 实现类
 *
 * @author ${table.author}
 */
@Service
@Validated
public class UmsAppVersionServiceImpl implements UmsAppVersionService {

    @Resource
    private UmsAppVersionMapper appVersionMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createAppVersion(UmsAppVersionSaveReqVO createReqVO) {
        // 插入
        UmsAppVersionDO appVersion = BeanUtils.toBean(createReqVO, UmsAppVersionDO.class);
        appVersionMapper.insert(appVersion);
        // 返回
        return appVersion.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAppVersion(UmsAppVersionSaveReqVO updateReqVO) {
        // 校验存在
        validateAppVersionExists(updateReqVO.getId());
        // 更新
        UmsAppVersionDO updateObj = BeanUtils.toBean(updateReqVO, UmsAppVersionDO.class);
        appVersionMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAppVersion(Long id) {
        // 校验存在
        validateAppVersionExists(id);
        // 删除
        appVersionMapper.deleteById(id);
    }

    private void validateAppVersionExists(Long id) {
        if (appVersionMapper.selectById(id) == null) {
            throw exception(DATA_NOT_FOUND);
        }
    }

    @Override
    public UmsAppVersionDO getAppVersion(Long id) {
        return appVersionMapper.selectById(id);
    }

    @Override
    public PageResult<UmsAppVersionDO> getAppVersionPage(UmsAppVersionPageReqVO pageReqVO) {
        return appVersionMapper.selectPage(pageReqVO);
    }

}
